using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;

using MakeIT.Framework.Data;
using MakeIT.Look.Imagem.Entidades;

namespace MakeIT.Look.Imagem.Negocios
{
	public partial class LookDAOT : DAOT
	{

		public BookclivideoVO getBookclivideo(int? idBookCliVideo, string nome)
		{
			BookclivideoVO bookclivideoVO = null;
			List<BookclivideoVO> listBookclivideoVOs = getBookclivideos(idBookCliVideo, nome, null);
			if (listBookclivideoVOs != null && listBookclivideoVOs.Count > 0)
			{
				bookclivideoVO = listBookclivideoVOs[0];
			}
			return bookclivideoVO;
		}

		public BookclivideoVO getBookclivideo(int? idBookCliVideo)
		{
			return getBookclivideo(idBookCliVideo, null);
		}

		public List<BookclivideoVO> getBookclivideos()
		{
			return getBookclivideos(null, null, null);
		}

		public List<BookclivideoVO> getBookclivideos(int? idBookCliVideo)
		{
			return getBookclivideos(idBookCliVideo, null, null);
		}

		public List<BookclivideoVO> getBookclivideos(int? idBookCliVideo, string nome)
		{
			return getBookclivideos(idBookCliVideo, nome, null);
		}

		public List<BookclivideoVO> getBookclivideos(int? idBookCliVideo, string nome, int? idBookCli)
		{
			string where = "WHERE ";
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT bcv.idBookCliVideo as bcv_idBookCliVideo, bcv.idBookCli as bcv_idBookCli, bcv.url as bcv_url, bcv.nome as bcv_nome, bcv.cadastro as bcv_cadastro, bcv.cadastradoPor as bcv_cadastradoPor, bcv.edicao as bcv_edicao, bcv.editadoPor as bcv_editadoPor  ");
			query.AppendLine("FROM bookclivideo as bcv LEFT OUTER JOIN bookcli as bcl on bcv.idBookCli=bcl.idBookCli  ");
			if (idBookCliVideo != null)
			{
				query.AppendLine(where + "bcv.idBookCliVideo=@idBookCliVideo ");
				where = "AND ";
			}
			if (idBookCli != null)
			{
				query.AppendLine(where + "bcv.idBookCli=@idBookCli ");
				where = "AND ";
			}
			if (nome != null)
			{
				query.AppendLine(where + "bcv.nome LIKE @nome ");
				where = "AND ";
			}


			query.AppendLine("ORDER BY bcv.nome ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			if (idBookCliVideo!= null) cmd.Parameters.Add(getParameter(cmd, "@idBookCliVideo", idBookCliVideo));
			if (idBookCli!= null) cmd.Parameters.Add(getParameter(cmd, "@idBookCli", idBookCli));
			if (nome!= null) cmd.Parameters.Add(getParameter(cmd, "@nome", "%"+nome+"%"));


			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();

				List<BookclivideoVO> listBookclivideo = new List<BookclivideoVO>();

				while (reader.Read())
				{
					BookclivideoVO bookclivideoVO = BookclivideoVO.CreateVO();
					bookclivideoVO.idBookCliVideo = reader.GetInt32(reader.GetOrdinal("bcv_idBookCliVideo"));
					bookclivideoVO.idBookCli = reader.GetInt32(reader.GetOrdinal("bcv_idBookCli"));
					bookclivideoVO.url = reader.GetString(reader.GetOrdinal("bcv_url"));
					bookclivideoVO.nome = reader.GetString(reader.GetOrdinal("bcv_nome"));
					bookclivideoVO.cadastro = reader.GetDateTime(reader.GetOrdinal("bcv_cadastro"));
					bookclivideoVO.cadastradoPor = reader.GetString(reader.GetOrdinal("bcv_cadastradoPor"));
					bookclivideoVO.edicao = reader.GetDateTime(reader.GetOrdinal("bcv_edicao"));
					bookclivideoVO.editadoPor = reader.GetString(reader.GetOrdinal("bcv_editadoPor"));


					listBookclivideo.Add(bookclivideoVO);
				}
				reader.Close();
				return listBookclivideo;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void setBookclivideo(BookclivideoVO bookclivideoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("UPDATE bookclivideo ");
			query.AppendLine("SET idBookCli=@idBookCli, url=@url, nome=@nome,  cadastro=@cadastro, cadastradoPor=@cadastradoPor, edicao=@edicao, editadoPor=@editadoPor ");
			query.AppendLine("WHERE idBookCliVideo=@idBookCliVideo ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idBookCliVideo", bookclivideoVO.idBookCliVideo));
			cmd.Parameters.Add(getParameter(cmd, "@idBookCli", bookclivideoVO.idBookCli));
			cmd.Parameters.Add(getParameter(cmd, "@url", bookclivideoVO.url));
			cmd.Parameters.Add(getParameter(cmd, "@nome", bookclivideoVO.nome));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", bookclivideoVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", bookclivideoVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", bookclivideoVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", bookclivideoVO.editadoPor));

			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}

		public long newBookclivideo(BookclivideoVO bookclivideoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("INSERT INTO bookclivideo ");
			query.AppendLine("  ( idBookCli, url, nome, cadastro, cadastradoPor, edicao, editadoPor ) ");
			query.AppendLine("VALUES ( @idBookCli, @url, @nome, @cadastro, @cadastradoPor, @edicao, @editadoPor ) ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idBookCli", bookclivideoVO.idBookCli));
			cmd.Parameters.Add(getParameter(cmd, "@url", bookclivideoVO.url));
			cmd.Parameters.Add(getParameter(cmd, "@nome", bookclivideoVO.nome));
			cmd.Parameters.Add(getParameter(cmd, "@cadastro", bookclivideoVO.cadastro));
			cmd.Parameters.Add(getParameter(cmd, "@cadastradoPor", bookclivideoVO.cadastradoPor));
			cmd.Parameters.Add(getParameter(cmd, "@edicao", bookclivideoVO.edicao));
			cmd.Parameters.Add(getParameter(cmd, "@editadoPor", bookclivideoVO.editadoPor));


			try
			{
				cmd.ExecuteNonQuery();

				long newID = 0;
				if (tipo.Equals(DBType.MSSql))
				{
					cmd.CommandText = "SELECT IDENT_CURRENT('bookclivideo') AS 'Identity' ";
					DbDataReader reader = cmd.ExecuteReader();
					reader.Read();
					newID = (long)reader.GetDecimal(reader.GetOrdinal("Identity"));
					reader.Close();
				}
				else if (tipo.Equals(DBType.MySql))
				{
					newID = ((MySql.Data.MySqlClient.MySqlCommand)cmd).LastInsertedId;
				}
				else if (tipo.Equals(DBType.ODBC))
				{
					//TODO implementar
				}
				else if (tipo.Equals(DBType.ORACLE))
				{
					//TODO implementar
				}
				return newID;
			}
			catch (Exception ex) { throw ex; }

		}

		public void delBookclivideo(BookclivideoVO bookclivideoVO)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("DELETE FROM bookclivideo ");
			query.AppendLine("WHERE idBookCliVideo=@idBookCliVideo ");

			DbCommand cmd = getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@idBookCliVideo", bookclivideoVO.idBookCliVideo));


			try
			{
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex) { throw ex; }

		}
		public int getCountBookclivideoxIdBookCli(int? idBookCli)
		{
			StringBuilder query = new StringBuilder();

			query.AppendLine("SELECT COUNT(idBookCli) as ocorrencias FROM bookclivideo WHERE IdBookCli=@IdBookCli ");

			DbCommand cmd = this.getCommand();
			cmd.CommandText = query.ToString();

			cmd.Parameters.Add(getParameter(cmd, "@IdBookCli", idBookCli));

			DbDataReader reader = null;
			try
			{
				reader = cmd.ExecuteReader();
				reader.Read();

				int ocorrencias = 0;
				ocorrencias = reader.GetInt32(reader.GetOrdinal("ocorrencias"));
				reader.Close();
				return ocorrencias;
			}
			catch (Exception ex) { throw ex; }
			finally
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}
	}
}
